package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.HatchetAcq;
import com.ruoyi.system.service.IHatchetAcqService;

/**
 * 通道 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-03-23
 */
@Controller
@RequestMapping("/system/hatchetAcq")
public class HatchetAcqController extends BaseController
{
    private String prefix = "system/hatchetAcq";
	
	@Autowired
	private IHatchetAcqService hatchetAcqService;
	
	@RequiresPermissions("system:hatchetAcq:view")
	@GetMapping()
	public String hatchetAcq()
	{
	    return prefix + "/hatchetAcq";
	}
	
	/**
	 * 查询通道列表
	 */
	@RequiresPermissions("system:hatchetAcq:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(HatchetAcq hatchetAcq)
	{
		startPage();
        List<HatchetAcq> list = hatchetAcqService.selectHatchetAcqList(hatchetAcq);
		return getDataTable(list);
	}
	
	/**
	 * 查询所有通道
	 */
	@RequiresPermissions("system:hatchetAcq:list")
	@PostMapping("/all")
	@ResponseBody
	public List<HatchetAcq> all(HatchetAcq hatchetAcq)
	{
		
		/*hatchetAcq.setEmployStatus("10A");*/
		return hatchetAcqService.selectHatchetAcqList(hatchetAcq);
	}
	/**
	 * 导出通道列表
	 */
	@RequiresPermissions("system:hatchetAcq:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HatchetAcq hatchetAcq)
    {
    	List<HatchetAcq> list = hatchetAcqService.selectHatchetAcqList(hatchetAcq);
        ExcelUtil<HatchetAcq> util = new ExcelUtil<HatchetAcq>(HatchetAcq.class);
        return util.exportExcel(list, "hatchetAcq");
    }
	
	/**
	 * 新增通道
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存通道
	 */
	@RequiresPermissions("system:hatchetAcq:add")
	@Log(title = "通道", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(HatchetAcq hatchetAcq)
	{		
		hatchetAcq.setId(StringUtils.randomUUID());
		String maxAcqCode =hatchetAcqService.getMaxAcqCode();
		if(StringUtils.isEmpty(maxAcqCode)){
			maxAcqCode="8000";
		}
		int acqCode =Integer.parseInt(maxAcqCode)+1;
		hatchetAcq.setAcqCode(""+acqCode);
		hatchetAcq.setEmployStatus("10A");
		hatchetAcq.setCreateUserId(getUserId()+"");//创建人ID
		hatchetAcq.setCreateTime(new Date());//创建时间
		hatchetAcq.setEndEditTime(new Date());
		return toAjax(hatchetAcqService.insertHatchetAcq(hatchetAcq));
	}

	/**
	 * 修改通道
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		HatchetAcq hatchetAcq = hatchetAcqService.selectHatchetAcqById(id);
		mmap.put("hatchetAcq", hatchetAcq);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存通道
	 */
	@RequiresPermissions("system:hatchetAcq:edit")
	@Log(title = "通道", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(HatchetAcq hatchetAcq)
	{	
		hatchetAcq.setEndEditTime(new Date());
		return toAjax(hatchetAcqService.updateHatchetAcq(hatchetAcq));
	}
	
	/**
	 * 删除通道
	 */
	@RequiresPermissions("system:hatchetAcq:remove")
	@Log(title = "通道", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(hatchetAcqService.deleteHatchetAcqByIds(ids));
	}
	
	/**
     * 通道使用状态修改
     */
    @Log(title = "通道管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:hatchetAcq:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
	public AjaxResult changeStatus(HatchetAcq hatchetAcq) {
		return toAjax(hatchetAcqService.updateHatchetAcq(hatchetAcq));
	}
}
